package net.lainiao.mycost.controller;

import com.sun.jersey.core.util.StringIgnoreCaseKeyComparator;
import net.lainiao.mycost.bean.AuditHistory;
import net.lainiao.mycost.bean.CostInfo;
import net.lainiao.mycost.bean.ExpenseAccount;
import net.lainiao.mycost.bean.UserInfo;
import net.lainiao.mycost.service.impl.ExpenseServiceImpl;
import net.lainiao.mycost.utils.Comm;
import net.lainiao.mycost.utils.Page;
import org.activiti.engine.impl.juel.ExpressionNode;
import org.springframework.beans.propertyeditors.CustomBooleanEditor;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.HttpSessionRequiredException;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017/3/22.
 */
@Controller
@RequestMapping("/expense/")
public class ExpenseController extends Page {

    @RequestMapping("loadadd.do")
    public String loadadd(Model model) {
        CostInfo costInfo = new CostInfo();
        costInfo.setCostMark(Comm.MARK_YES);
        costInfo.setStart(-1);
        List<CostInfo> list = costInfoService.list(costInfo);
        model.addAttribute("list", list);
        return "expense/expense_add";
    }

    @RequestMapping("add.do")
    public String add(HttpSession session, ExpenseAccount account, Integer[] costIds, Double[] expenseDetailsSmount, Model model) {
        UserInfo userInfo = (UserInfo) session.getAttribute("user");
        if (account != null) {
            account.setUserId(userInfo.getUserId());
            account.setExpenseState(Comm.EXPPENSE_STATE_ONE);
        }
        expenseService.add(account, costIds, expenseDetailsSmount);
        model.addAttribute("errorinfo", "报销单提交成功,请等待审核");
        return loadadd(model);
    }

    @RequestMapping("loadAuditManager.do")
    public String loadAuditManager(ExpenseAccount account, Model model, HttpServletRequest request) {
        if (account == null) {
            account = new ExpenseAccount();
        }
        account.setExpenseState(Comm.EXPPENSE_STATE_ONE);
        this.initPage(request);
        account.setStart(this.getPageNo());
        account.setLength(PAGE_NUM_BIG);
        model.addAttribute("list", expenseService.list(account));
        model.addAttribute("total", expenseService.getCount(account));
        return "expense/expense_audit_manager";
    }

    @RequestMapping("loadManager.do")
    public String loadManager(ExpenseAccount account, Model model) {
        ExpenseAccount expenseAccount = expenseService.getExpenseAccount(account);
        List<Map> list = expenseService.getExpenseDetailsList(account);
        model.addAttribute("exp", expenseAccount);
        model.addAttribute("list", list);
        return "expense/expense_manager";
    }

    @RequestMapping("auditManger.do")
    public String auditManager(AuditHistory history, Model model, HttpServletRequest request, HttpSession session) {
        UserInfo userInfo = (UserInfo) session.getAttribute("user");
        if (history != null) {
            history.setUserId(userInfo.getUserId());
        }
        expenseService.auditManager(history);
        model.addAttribute("errorinfo", "报销单审核成功");
        return loadAuditManager(null, model, request);
    }

    @RequestMapping("loadAuditFinance.do")
    public String loadAuditFinance(ExpenseAccount account, Model model, HttpServletRequest request) {
        if (account != null) {
            account = new ExpenseAccount();
        }
        account.setExpenseState(Comm.EXPPENSE_STATE_TWO);
        this.initPage(request);
        account.setStart(this.getPageNo());
        account.setLength(PAGE_NUM_BIG);
        model.addAttribute("list", expenseService.list(account));
        model.addAttribute("total", expenseService.getCount(account));
        return "expense/expense_audit_finance";
    }

    @RequestMapping("loadFinance.do")
    public String loadFinance(ExpenseAccount account, Model model) {
        ExpenseAccount expenseAccount = expenseService.getExpenseAccount(account);
        List<Map> list = expenseService.getExpenseDetailsList(account);
        List<Map> historyList = expenseService.getAuditHistoryList(account);
        model.addAttribute("exp", expenseAccount);
        model.addAttribute("list", list);
        model.addAttribute("historyList", historyList);
        return "expense/expense_finance";
    }

    @RequestMapping("auditFinance.do")
    public String auditFinance(AuditHistory history, Model model, HttpServletRequest request, HttpSession session) {
        UserInfo userInfo = (UserInfo) session.getAttribute("user");
        if (history != null) {
            history.setUserId(userInfo.getUserId());
        }
        expenseService.auditFinance(history);
        model.addAttribute("errorinfo", "报销单审核成功");
        return loadAuditFinance(null, model, request);
    }

    @RequestMapping("mylist.do")
    public String mylist(ExpenseAccount account, Model model, HttpServletRequest request, HttpSession session) {
        if (account == null) {
            account = new ExpenseAccount();
        }
        UserInfo userInfo = (UserInfo) session.getAttribute("user");
        account.setUserId(userInfo.getUserId());
        this.initPage(request);
        account.setStart(this.getPageNo());
        account.setLength(PAGE_NUM_BIG);
        model.addAttribute("list", expenseService.list(account));
        model.addAttribute("total", expenseService.getCount(account));
        return "expense/expense_mylist";
    }

    @RequestMapping("myshow.do")
    public String myshow(ExpenseAccount account, Model model) {
        ExpenseAccount expenseAccount = expenseService.getExpenseAccount(account);
        List<Map> list = expenseService.getExpenseDetailsList(account);
        List<Map> historyList = expenseService.getAuditHistoryList(account);
        model.addAttribute("exp", expenseAccount);
        model.addAttribute("list", list);
        model.addAttribute("historyList", historyList);
        return "expense/expense_myshow";
    }

    @RequestMapping("loadupdate.do")
    public String loadupdate(ExpenseAccount account, Model model) {
        ExpenseAccount expenseAccount = expenseService.getExpenseAccount(account);
        model.addAttribute("exp", expenseAccount);
        List<Map> list = expenseService.getExpenseDetailsList(account);
        model.addAttribute("list", list);
        CostInfo costInfo=new CostInfo();
        costInfo.setCostMark(Comm.MARK_YES);
        costInfo.setStart(-1);
        List<CostInfo> costList=costInfoService.list(costInfo);
        model.addAttribute("costList",costList);
        return "expense/expense_update";
    }

    @RequestMapping("update.do")
    public String update(HttpServletRequest request,HttpSession session,ExpenseAccount account,Integer[] costIds,Double[] expenseDetailsSmount,Model model){
        UserInfo userInfo=(UserInfo) session.getAttribute("user");
        if(account!=null){
            account.setUserId(userInfo.getUserId());
            account.setExpenseState(Comm.EXPPENSE_STATE_ONE);
        }
        expenseService.update(account,costIds,expenseDetailsSmount);
        model.addAttribute("errorinfo","报销单提交成功,请等待审核!");
        return mylist(null,model,request,session);
    }

    @RequestMapping("list.do")
    public String list(ExpenseAccount account, Model model,HttpServletRequest request){
        if(account==null){
            account=new ExpenseAccount();
        }
        this.initPage(request);
        account.setStart(this.getPageNo());
        account.setLength(PAGE_NUM_BIG);
        model.addAttribute("list",expenseService.list(account));
        model.addAttribute("total",expenseService.getCount(account));
        return "expense/expense_list";
    }

    @RequestMapping("show.do")
    public String show(ExpenseAccount account,Model model){
        ExpenseAccount expenseAccount= expenseService.getExpenseAccount(account);
        List<Map> list=expenseService.getExpenseDetailsList(account);
        List<Map> historyList=expenseService.getAuditHistoryList(account);
        model.addAttribute("exp",expenseAccount);
        model.addAttribute("list",list);
        model.addAttribute("historyList",historyList);
        return "expense/expense_show";
    }

    @RequestMapping("myauditlist.do")
    public String myauditlist(HttpServletRequest request,ExpenseAccount account,HttpSession session,Model model){
        if(account!=null){
            account=new ExpenseAccount();
        }
        this.initPage(request);
        account.setStart(this.getPageNo());
        account.setLength(PAGE_NUM_BIG);
        UserInfo userInfo=(UserInfo)session.getAttribute("user");
        account.setUserId(userInfo.getUserId());
        List<ExpenseAccount> list=expenseService.getList(account);
        model.addAttribute("list",list);
        model.addAttribute("total",expenseService.getCount(account));
        return "expense/expense_myauditlist";
    }

    @RequestMapping("myauditshow.do")
    public String myauditshow(ExpenseAccount account,Model model){
        ExpenseAccount expenseAccount=expenseService.getExpenseAccount(account);
        List<Map> list=expenseService.getExpenseDetailsList(account);
        List<Map> historyList=expenseService.getAuditHistoryList(account);
        model.addAttribute("exp",expenseAccount);
        model.addAttribute("list",list);
        model.addAttribute("historyList",historyList);
        return "expense/expense_myauditshow";
    }

    @InitBinder
    public void initBinder(ServletRequestDataBinder bin){
        SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd");
        bin.registerCustomEditor( Date.class,new CustomDateEditor(format,true));
    }
}
